package com.ajie.jdbc;

import com.ajie.jdbc.dao.UserDao01;
import com.ajie.jdbc.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author wyj
 * @since 2022/1/20
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        /**
         * 手工注册接口到configuration中，最终通过扫描的方式也是会注册这一步；
         */
        sqlSessionFactory.getConfiguration().addMapper(UserDao01.class);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /**
         * 因为没有对应的UserDao01对应的mapper.xml,所以执行会有未注册的提示（没有上一步注册的前提下）
         * 那么需要手动注册接口：
         * sqlSessionFactory.getConfiguration().addMapper(UserDao01.class)
         */
        UserDao01 userDao = sqlSession.getMapper(UserDao01.class);
        User user = userDao.selectUser(4);
        System.out.println(user);
    }
}
